package org.cwnuit.train.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.cwnuit.train.domain.Learns;
import org.cwnuit.train.domain.Rules;
import org.cwnuit.train.domain.Trains;
import org.cwnuit.train.domain.Wtype;
import org.cwnuit.train.utils.ConnDB;
import org.cwnuit.train.utils.EntityUtil;
import org.cwnuit.train.utils.PageModel;
@SuppressWarnings("all")
public class WtypeDAO {
	
	private static Logger logger = Logger.getLogger(WtypeDAO.class.getName());   

	public boolean addWtype(String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		boolean flag = false;
		try {
			stmt.executeUpdate(SQL);
			flag = true;
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeConnection(conn);
		}
		return flag;
	}

	public boolean deleteWtype(String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		boolean flag = false;
		try {
			stmt.execute(SQL);
			flag = true;
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeConnection(conn);
		}
		return flag;
	}

	public boolean updateWtype(String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		boolean flag = false;
		try {
			stmt.executeUpdate(SQL);
			flag = true;
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeConnection(conn);
		}
		return flag;
	}
	
	// 查找Wtype的方法
	public Wtype findWtype(String SQL) {

		Wtype wtype = null;
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		ResultSet rs = ConnDB.getResultSet(stmt, SQL);

		try {
			while (rs.next()) {
				wtype = EntityUtil.RsToWtype(rs);
			}
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeResultSet(rs);
			ConnDB.closeConnection(conn);
		}
		return wtype;
	}
	// 查找Wtype的方法
	public List<Wtype> findWtypes(String SQL) {
		List<Wtype> wtypeslist = new ArrayList();
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		ResultSet rs = ConnDB.getResultSet(stmt, SQL);

		try {
			while (rs.next()) {
				wtypeslist.add(EntityUtil.RsToWtype(rs));
			}
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeResultSet(rs);
			ConnDB.closeConnection(conn);
		}
		return wtypeslist;
	}
	
	public PageModel findByPage(int offset, int pagesize, String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		String SQL1 = SQL + " LIMIT " + offset + "," + pagesize;
		ResultSet rs = ConnDB.getResultSet(stmt, SQL1);

		PageModel pm = new PageModel();
		List<Wtype> wtypeslist = new ArrayList();
		try {
			while (rs.next()) {
				wtypeslist.add(EntityUtil.RsToWtype(rs));
			}
			pm.setTotal(this.countNum(SQL));
			pm.setDatas(wtypeslist);
			logger.debug(SQL1);
		} catch (SQLException e) {
			e.printStackTrace();

		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeResultSet(rs);
			ConnDB.closeConnection(conn);
		}
		return pm;
	}
	
	// 查找数据库中的记录数
	public int countNum(String SQL) {
		Connection conn = ConnDB.getConnection();
		Statement stmt = ConnDB.getStatement(conn);
		ResultSet rs = ConnDB.getResultSet(stmt, SQL);
		int counter = 0;
		try {
			while (rs.next()) {
				counter += 1;
			}
			logger.debug(SQL);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnDB.closeStatement(stmt);
			ConnDB.closeResultSet(rs);
			ConnDB.closeConnection(conn);
		}
		return counter;
	}


}
